﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Kotonoha.Server.Utils;
using log4net;
using NHibernate;
using NHibernate.Cfg;

namespace Kotonoha.Server.Core.Utils {
    public class HibernateUtil {
        private readonly ILog log = LogManager.GetLogger(typeof (HibernateUtil));
        private delegate void ResourcesMuncherDelegate(Configuration cfg);

        private static object SynchRoot = new object();

        private static HibernateUtil model;
        private static HibernateUtil dict;
        public static HibernateUtil Model {
            get {
                lock (SynchRoot) {
                    if (model == null) {
                        model = new HibernateUtil(AddModelResources, PathUtil.DB_PATH);
                    }
                    return model;
                }
            }
        }

        private static void AddModelResources(Configuration cfg) {
            cfg.AddResource("Kotonoha.Server.mappings.model.hbm.xml", Assembly.GetAssembly(typeof(App)));
        }

        public static HibernateUtil Dictionary {
            get {
                lock (SynchRoot) {
                    if (dict == null) {
                        dict = new HibernateUtil(AddDicResources, PathUtil.DB_DIC_PATH);
                    }
                    return dict;
                }
            }
        }

        private static void AddDicResources(Configuration cfg) {
            cfg.AddResource("Kotonoha.Server.mappings.dictionaries.hbm.xml", Assembly.GetAssembly(typeof(App)));
        }

        private ISessionFactory factory;

        private HibernateUtil(ResourcesMuncherDelegate del, string path) {
            log.Info(string.Format("Creating Hibernate SessionFactory for {0}", path));
            Configuration cfg = new Configuration();
            cfg.Configure("hibernate.cfg.xml");
            cfg.SetProperty("connection.connection_string", string.Format("Data Source={0}", path));
            //cfg.AddAssembly("Kotonoha");
            del(cfg);
            factory = cfg.BuildSessionFactory();
        }

        public static void shutdown() {
            if (model != null) {
                Close(model);
            }
            if (dict != null) {
                Close(dict);
            }
        }

        private static void Close(HibernateUtil hu) {
            if (hu.session != null) {
                hu.session.Close();
            }
            if (hu.factory != null) {
                hu.factory.Close();
            }
        }

        private ISession session;
        

        public ISession Session {
            get {
                if (session == null) {
                    session = createSession();
                }
                return session;
            }
        }

        public IStatelessSession createStatelessSession() {
            return factory.OpenStatelessSession();
        }

        public ISession createSession() {
            return factory.OpenSession();
        }
    }
}
